let crypto = require("crypto")
var request = require("request")
let jsonwebtoken = require("jsonwebtoken")
var getServer = require("../debug/test.eff").getServer


function md5(str) {
	let md5 = crypto.createHash("md5")
	md5.update(str)
	return md5.digest("hex").toUpperCase()  //32位大写
}

var url = "http://127.0.0.1:7002"
var token = "prom_token " + getServer({id: 11, openid: "osGf64pPQrwNPuRwibhaqkReURDs"})
console.log(url)

var options = {
	method: "POST",
	url: `${url}/game1/sell`,
	headers:
	{
		"cache-control": "no-cache",
		authorization: token,
		"Content-Type": "application/x-www-form-urlencoded",
	}
}


async function updateUserData() {
	let d = await getUserData()
	console.log(d)
	let body = {
		"total_coin": d.total_coin, // 总资产
		"popularity": d.popularity, // 声望
		"sycee": d.sycee, // 金元宝
	}
	body.popularity = 10000
	body.total_coin = 1e21
	body.sycee = 1000
	console.log(body)
	let arrMap = ["total_coin", "popularity", "sycee"]
	let hash = arrMap.map((str) => {
		return str + ":" + body[str]
	}).join("\n") + "\ntime:" + (Date.now() / 1000 / 60 | 0)
	hash = md5(hash)

	request({
		...options, form: {
			...body, hash
		},
		url: `${url}/game1/updateUserData`,
	}, function (error, response, body) {
		console.log(body)
		if (error) throw new Error(error)
		getUserData().then(console.log)
		// console.log(body)
	})
}

function getUserData() {
	return new Promise((reslove, reject) => {
		request({
			...options,
			method: "GET",
			url: `${url}/game1/getUserData`,
		}, function (error, response, body) {
			if (error) throw new Error(error)
			reslove(JSON.parse(body).d)
		})
	})
}
updateUserData()